<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
class Carousel extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("name")) {
            $map['name'] = ["like", "%" . $this->request->param("name") . "%"];
        }
    }
    public function index(){
    	$model = $this->getModel();
    	// 列表过滤器，生成查询Map对象
    	// $map = $this->search($model, [$this->fieldIsDelete => $this::$isdelete]);

    	// 特殊过滤器，后缀是方法名的
    	$actionFilter = 'filter' . $this->request->action();
    	if (method_exists($this, $actionFilter)) {
    	    $this->$actionFilter($map);
    	}

    	// 自定义过滤器
    	if (method_exists($this, 'filter')) {
    	    $this->filter($map);
    	}

        $map['s.isdelete']=0;
    	$limit=12;
    	$list=Db::table('tp_carousel')->alias('s')
    						   ->join('img i','s.id=i.relation_id and i.isdelete=0 and i.folder=\'carousel\'','LEFT')
    						   ->field('s.*,i.url img_url')
    						   ->where($map)
    						   ->order('sort desc')
    						   ->paginate($limit);
    	// $list=$model->where($map)->paginate(12);


    	$list2=$list->toArray();
    	foreach ($list2['data'] as &$val) {
    		$val['img']=imghandle($val['img_url'],'md_');
    	}

    	$p=input('get.p')?:1;
        $offset=($p-1)*$limit+1;
    	$this->view->assign(['list'=>$list2['data'],'page'=>$list->render(),'offset'=>$offset]);
    	// $this->datalist($model, $map);
    	return $this->view->fetch();
    }
    /**
    *添加方法
    *Carousel/add
    *
    *2017.12.20
    *BY HF
    */
    public function add(){
    	$controller = $this->request->controller();
    	if($this->request->isAjax()){
    		// $param=$this->request->post(); 
    		$param = $this->request->except(['id']);
    		$param['create_time']=date('Y-m-d H:i:s');
    		if(!isset($param['img_id'])){
	    		Db::name('carousel')->insertGetId($param);
    			return ajax_return_adv("编辑成功");
    		}
    		$img_id=$param['img_id']; //图片表中的ID
    		unset($param['img_id']);
    		Db::startTrans();
    		try {
    		    // 提交事务
    			$relation_id=Db::name('carousel')->insertGetId($param);
    			Db::name('img')->update(['id'=>$img_id,'relation_id'=>$relation_id]);
    		    Db::commit();
    		} catch (\Exception $e) {
    		    // 回滚事务
    		    Db::rollback();
    		    return ajax_return_adv_error($e->getMessage());
    		}
	    	return ajax_return_adv("编辑成功");
    	}else{
	    	return $this->view->fetch(isset($this->template) ? $this->template : 'edit');
    	}
    }
    /**
    *编辑方法
    *Carousel/edit
    *2017.12.20
    *BY HF
    */
    public function edit(){
    	$controller = $this->request->controller();
    	if($this->request->isAjax()){
			$param=$this->request->post(); 
			if(!isset($param['img_id'])){
				Db::name('carousel')->update($param);
				return ajax_return_adv("编辑成功");
			}
			$imgid=$param['img_id'];
			unset($param['img_id']);
			
			Db::startTrans();
			try {
			    // 提交事务
				Db::name('carousel')->update($param);
				Db::name('img')->update(['id'=>$imgid,'relation_id'=>$param['id']]);
			    Db::commit();
			} catch (\Exception $e) {
			    // 回滚事务
			    Db::rollback();
			    return ajax_return_adv_error($e->getMessage());
			}
			return ajax_return_adv("编辑成功");
    	}else{
    		$id = $this->request->param('id');
    		if (!$id) {
    		    throw new HttpException(404, "缺少参数ID");
    		}
    		$vo = $this->getModel($controller)->find($id);
    		$vo=Db::table('tp_carousel')->alias('s')
    							   ->join('img i','s.id=i.relation_id and i.folder=\'carousel\' and s.isdelete=0 and i.isdelete=0','LEFT')
    							   ->field('s.*,i.url img_url,i.id imgid')
    							   ->find($id);
    		if (!$vo) {
    		    throw new HttpException(404, '该记录不存在');
    		}
    		$vo['img']=imghandle($vo['img_url'],'sm_');
    		$vo['mdimg']=imghandle($vo['img_url'],'md_');
    		$this->view->assign("vo", $vo);
    		return $this->view->fetch();	
    	}
    }
}
